﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLMonHoc
    {
        List<MonHoc> list;
        QuanLySVDataContext db;
        

        // constructor
        public BLLMonHoc()
        {
            this.list = new QuanLySVDataContext().MonHocs.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 mon hoc co ton tai hay khong
        public bool checkExist(MonHoc temp)
        {
            foreach (MonHoc dt in list)
            {
                if (dt.MonHocID.Equals(temp.MonHocID))
                    return true;
            }
            return false;
        }

        // update thong tin mon hoc
        public bool updateMonHoc(MonHoc update)
        {
            try
            {                

                var temp = (from c in db.MonHocs
                            where c.MonHocID == update.MonHocID
                            select c).First();
                MonHoc found = (MonHoc)temp;
                found.MonHocID = update.MonHocID;
                found.TenMonHoc = update.TenMonHoc;
                found.GhiChu = update.GhiChu;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them mon hoc moi vao database
        public bool insertMonHoc(MonHoc temp)
        {
            try
            {
                db.MonHocs.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa mon hoc
        public bool deleteMonHoc(MonHoc temp)
        {
            try
            {
                var dt = (from c in db.MonHocs
                          where c.MonHocID == temp.MonHocID
                          select c).First();
                MonHoc found = (MonHoc)dt;

                db.MonHocs.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac mon hoc
        public List<MonHoc> returnListMonHoc()
        {
            this.list = new QuanLySVDataContext().MonHocs.ToList();
            return list;
        }

        public List<string> getListTenMonHoc()
        {
            List<string> result = new List<string>();
            this.list = new QuanLySVDataContext().MonHocs.ToList();
            foreach (MonHoc item in list)
            {
                result.Add(item.TenMonHoc);
            }
            return result;
        }

        // tra ve 1 mon hoctheo so thu tu trong list
        public MonHoc retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().MonHocs.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        public string retrieveIDByName(string tenMonHoc)
        {
            var monID = (from item in db.MonHocs
                         where item.TenMonHoc == tenMonHoc
                         select item).Single();
            return monID.MonHocID;
        }

        // tra ve 1 mon hoctheo ma mon hoc
        public MonHoc retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().MonHocs.ToList();
            foreach (MonHoc temp in list)
                if (temp.MonHocID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 mon hoc co hop le hay khong
        public bool kiemTra(MonHoc temp)
        {
            if (temp.MonHocID == "") return false;
            return true;
        }
    }
}
